package application.gui.main.startWindow;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import application.CommunicationModule.Communication;

public class Homepage extends JFrame {

	public Homepage() {
		initComponents();
	}

	private void initComponents() {
		setSize(600, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("ePaczka");
		setResizable(false);
		setLayout(null);
		// init panels
		initMainPanels();
		initButtons();
		
		addComponent(startPanel, welcomeLabel, 150, 140, 300, 50);
		loginPanel = new LoginPanel(this);
		registerPanel = new RegisterPanel(this);
		add(registerPanel);
		registerPanel.setBounds(0, 100, 600, 500);
		registerPanel.setVisible(false);
		
		createAndShowStatusPanel();
		createAndShowMenuPanel();
		setActionListeners();
		
		add(northPanel);
		northPanel.setBounds(0, 0, 600, 40);
		add(startPanel);
		startPanel.setBounds(0, 40, 600, 360);
		
	}

	private void addComponent(JPanel panel, JComponent comp, Rectangle r) {
		panel.add(comp);
		comp.setBounds(r);
	}

	private void addComponent(JPanel panel, JComponent comp, int x, int y,
			int width, int height) {
		panel.add(comp);
		comp.setBounds(x, y, width, height);
	}

	private void initMainPanels() {
		startPanel = new JPanel();
		startPanel.setSize(600, 360);		
		northPanel = new JPanel();
		northPanel.setSize(600, 40);
		northPanel.setLayout(null);
		startPanel.setLayout(null);
	}

	private void initButtons() {
		loginButton = new JButton("Zaloguj się");
		registerButton = new JButton("Zarejestruj się");
		checkDeliveryStatusButton = new JButton("Sprawdź status przesyłki");
		welcomeLabel = new JLabel("Witamy w systemie zarządzania paczkami.");
		
		
		
	}


	

	

	private void createAndShowStatusPanel() {
		deliveryPanel = new JPanel();
		deliveryPanel.setSize(600, 360);
		deliveryPanel.setLayout(null);
		deliveryPanel.setVisible(false);
		deliveryInput = new JTextField();
		checkDelivery = new JButton("Sprawdź");
		deliveryInfo = new JLabel();
		addComponent(deliveryPanel, deliveryInfo, 200, 80, 400, 30);
		addComponent(deliveryPanel, deliveryInput, 225, 120, 150, 25);
		addComponent(deliveryPanel, checkDelivery, 250, 160, 100, 20);
	}
	
	private void createAndShowMenuPanel(){
		addComponent(northPanel, loginButton, new Rectangle(350, 5, 100, 25));
		addComponent(northPanel, registerButton, new Rectangle(460, 5, 120, 25));
		addComponent(northPanel, checkDeliveryStatusButton, new Rectangle(15,
				5, 200, 25));
	}

	private void setActionListeners() {
		loginButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				clearFields();
				if (startPanel.isVisible()) {
					startPanel.setVisible(false);
					remove(startPanel);
				}
				if (registerPanel.isVisible()) {
					registerPanel.setVisible(false);
					remove(registerPanel);
				}
				if (deliveryPanel.isVisible()) {
					deliveryPanel.setVisible(false);
					remove(deliveryPanel);
				}
				if (!loginPanel.isVisible()) {
					loginPanel.setVisible(true);
					add(loginPanel);
					loginPanel.setBounds(0, 40, 600, 360);
					repaint();
					validate();
				}

			}
		});

		checkDeliveryStatusButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				clearFields();
				if (startPanel.isVisible()) {
					startPanel.setVisible(false);
					remove(startPanel);
				}
				if (registerPanel.isVisible()) {
					registerPanel.setVisible(false);
					remove(registerPanel);
				}
				if (loginPanel.isVisible()) {
					loginPanel.setVisible(false);
					remove(loginPanel);
				}
				if (!deliveryPanel.isVisible()) {
					deliveryPanel.setVisible(true);
					add(deliveryPanel);
					deliveryPanel.setBounds(0, 40, 600, 360);
					repaint();
					validate();
				}

			}
		});

		
		registerButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				clearFields();
				if (startPanel.isVisible()) {
					startPanel.setVisible(false);
					remove(startPanel);
				}
				if (deliveryPanel.isVisible()) {
					deliveryPanel.setVisible(false);
					remove(deliveryPanel);
				}
				if (loginPanel.isVisible()) {
					loginPanel.setVisible(false);
					remove(loginPanel);
				}
				if (!registerPanel.isVisible()) {
					registerPanel.setVisible(true);
					add(registerPanel);
					registerPanel.setBounds(0, 100, 600, 500);
					repaint();
					validate();
				}
			}
		});
		
		checkDelivery.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					db = new Communication("inzynier_admin", "admin");
					String status = db.getParcelStatus(Integer.parseInt(deliveryInput.getText()));
					deliveryInfo.setVisible(true);
					deliveryInfo.setText("Status: "+status);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
	}
	
	public void registerMsg(){
		registerPanel.setVisible(false);
		loginPanel.setVisible(true);
		add(loginPanel);
		loginPanel.setErrorInfoText("Rejestracja zakończona, możesz się zalogować");
		repaint();
		validate();
	}
	
	public void clearFields(){
		deliveryInput.setText("");
		deliveryInfo.setText("");
		registerPanel.clearFields();
		loginPanel.clearFields();
	}

	//Panel górny
	private JPanel northPanel;
	private JButton loginButton;
	private JButton registerButton;
	private JButton checkDeliveryStatusButton;
	
	//Panel startowy
	private JPanel startPanel;
	private JLabel welcomeLabel;
	
	//Panel logowania
	private LoginPanel loginPanel;

	
	//Panel sprawdzania statusu
	private JPanel deliveryPanel;
	private JButton checkDelivery;
	private JTextField deliveryInput;
	private JLabel deliveryInfo;
	
	private RegisterPanel registerPanel;
	Communication db;
	
	

}
